from core.decorators.open_api import open_api
from views.model import Customer
from core.base.base_model import DataModelFactory


class CommonService:
    @open_api(name="say.hello")
    def say_hello(self, hello=None):
        print(hello)
        # 可以查询数据能操作
        # Customer.batch_add([{"name":"小明","age":18,"sex":"male"}])
        names = Customer.get_customers_by_sex("male")
        result = ""
        for _name in names:
            result += f",{_name[0]}"
        return result

    @open_api(name="common.model.get")
    def common_get(self, model, id_, is_stand=False):
        return DataModelFactory.get_model(model).super_get_json(id_, is_stand=is_stand)

    @open_api(name="common.model.add")
    def common_add(self, model, info):
        _model = DataModelFactory.get_model(model)
        result = _model.create_data(info)
        return result

    @open_api(name="common.model.remove")
    def common_remove(self, model, id_):
        _model = DataModelFactory.get_model(model)
        result = _model.remove_data(id_)
        return result

    @open_api(name="common.model.modify")
    def common_modify(self, model, id_, info):
        _model = DataModelFactory.get_model(model)
        result = _model.update_data(id_, info)
        return result

    @open_api(name="common.model.list")
    def common_list(self):
        pass

    @open_api(name="common.test")
    def common_test(self):
        from core.cache.redis_cache import RedisService
        RedisService.setStrRedis("hello","workd",60*3)
        return RedisService.getStrRedis("hello")
